/*
 * @Author: xiaohao
 * @Date: 2025-11-03 09:08:13
 * @LastEditors: xiaohao
 * @LastEditTime: 2025-11-05 22:11:46
 * @FilePath: /Project/ConcurrentMemoryPool/include/ConcurrentMemoryPool/CentralCache.h
 * @Description:
 */

#pragma once

#include "Common.h"
#include "PageCache.h"

// 单例模式 单例饿汉
class CentralCache
{
    // 声明 ObjectPool<ThreadCache> 为友元类
    template <class T>
    friend class ObjectPool;

public:
    static CentralCache &GetCentralInstance();

    // 从中心缓存获取一定数量的对象给ThreadCache
    size_t FetchRangePbj(void *&start, void *&end, size_t n, size_t byte_size);

    // 获取一个非空的Span
    Span *GetOneSpan(SpanList &spanList, size_t size);

    // 将一定数量的对象释放到span跨度
    void ReleaseListToSpans(void *start, size_t size);

private:
    SpanList _spanList[NFREE_LISTS]; // 自由链表数组
    

private:
    // 私有构造函数，确保只能通过GetInstance获取
    CentralCache() = default;
    ~CentralCache() = default;

    // 禁止拷贝
    CentralCache(const CentralCache &) = delete;
    CentralCache &operator=(const CentralCache &) = delete;
};